package com.larry.resend.common;

import java.util.List;

import com.pegatron.ers.message.transaction.support.MessageTransactionManager;
import com.pegatron.ers.server.app.sub.core.ISubProcess;

public abstract class CommonSender {

	public String primaryClazz;
	public String primaryMethod;
	public String senderClass;
	public String consumerClass;
	public List<Object> initParams;

	public CommonSender(String primaryClazz, String primaryMethod, String senderClass, String consumerClass) {
		this.primaryClazz = primaryClazz;
		this.primaryMethod = primaryMethod;
		this.senderClass = senderClass;
		this.consumerClass = consumerClass;
		this.initData();
	}

	/**
	 * Main method
	 * 
	 * @throws Exception
	 */
	public abstract void process(String key) throws Exception;

	public abstract void initData();

	public void sendMessage() throws Exception {
		this.prepareParams();
		try {
			ISubProcess targetObject = (ISubProcess) Class.forName(senderClass).newInstance();
			targetObject.setMessageCriteria(consumerClass, null, null);
			targetObject.init(primaryClazz, primaryMethod, initParams);
			targetObject.proceed();
			MessageTransactionManager.getInstance().commit();
			System.out.println("Resend Success!!!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public abstract void prepareParams() throws Exception;
}
